package com.itheima.controller;

import com.itheima.domain.PageResult;
import com.itheima.domain.Result;
import com.itheima.domain.pojo.Students;
import com.itheima.domain.pojo.StudentsQueryParam;
import com.itheima.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentsController {

    @Autowired
    private StudentsService studentsService;
    //分页查询学生信息
    @GetMapping
    public Result page (StudentsQueryParam studentsQueryParam){
        log.info("分页查询：studentsQueryParam{}", studentsQueryParam);
        PageResult<Students> pageResult = studentsService.page(studentsQueryParam);
        return Result.success(pageResult);
    }
    //批量删除学生
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable List<Integer>  id){
        log.info("删除学生信息：id{}", id);
        studentsService.delete(id);
        return Result.success();
    }
    //添加学生
    @PostMapping
    public Result add(@RequestBody Students students){
        log.info("添加学生信息：students{}", students);
        studentsService.add(students);
        return Result.success();
    }
    //根据id查询学生信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询学生信息：id{}", id);
        Students students = studentsService.getById(id);
        return Result.success(students);
    }
    //根据id修改学生信息
    @PutMapping
    public Result update(@RequestBody Students students){
        log.info("修改学生信息：students{}", students);
        studentsService.update(students);
        return Result.success();
    }
    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score){
        log.info("违纪处理：id{}", id);
        studentsService.violation(id, score);
        return Result.success();
    }
}
